from datetime import datetime

from qturboframe.lib.utils.by import By
from qturboframe.lib.utils.assertion import Assertion
from qturboframe.lib.utils.CustomError.custom_error import CustomError
from qturboframe.lib.ranorex.client import CsClient
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.win_control.text import Text as WinText

from qturboframe.public_ui.ucf_wpf.basecontrol import BaseControl
from qturboframe.public_ui.ucf_wpf.button import Button


class MessageBox(BaseControl):
    """
    弹出信息框

    :Attributes:
     - _driver: 控件所在的当前Control对象
     - _control: 控件元素Control对象
     - message_box: 当前的弹出信息框对象
     - timeout: 步骤操作的超时时间

     :Raises
        CsClientError 没有找到控件异常

    :Example:
     - 例1: MessageBox({By.XPATH:"/DateTime"})
     - 例2: MessageBox({By.AUTOMATIONID:'automationid'})
     - 例3: MessageBox(control)
     - 例4: MessageBox({By.TEXT: 'Click'}, father=client)
     - 例5: MessageBox({By.AUTOMATIONID:'automationid'}, father=client)
     - 例6: MessageBox(control, father=client)
    """

    def __init__(self, element_para, father=None, timeout=10000):
        if isinstance(element_para, dict) and By.TITLE in element_para:
            cs = CsClient()
            xpath = "/form[@title='{}' and @visible='True']".format(element_para.get(By.TITLE))
            if cs.is_element_exist(xpath, time=timeout):
                self.message_box = cs.find_control_by_path(xpath, time=timeout)
                super().__init__(self.message_box, timeout=timeout)
                return

            xpath = "/form//text[@text='{}']".format(element_para.get(By.TITLE))
            if cs.is_element_exist(xpath, time=timeout):
                self.message_box = cs.find_control_by_path(xpath, time=timeout)
                super().__init__(self.message_box, timeout=timeout)
                return
            else:
                raise CsClientError(CustomError.Error_EQ03001000_Cannot_Find_Element,"没有找到名为:{}的弹出框".format(element_para.get(By.TITLE)))

        else:
            super().__init__(element_para, father, timeout)
            self.message_box = self._control

    def get_message(self):
        '''
        ucf wpf获取弹出消息框上的信息

        :Args:
         - None

        :Example:
         - 例: message_box.get_message()
        '''
        xpath = ".//text[@automationid='PART_Message' and @visible='True']//text"
        message = self.message_box.find_element_by_attribute(By.XPATH, xpath, timeout=self.timeout)
        text_control = WinText(message.control)
        if text_control:
            return text_control.get_text()
        else:
            raise CsClientError(CustomError.Error_EQ03001000_Cannot_Find_Element,"在弹出的信息窗口上没有找到文本控件")

    def check(self, expected_message, button_text="确定"):
        '''
        ucf wpf检查弹出消息框上的信息

        :Args:
         - expected_message：期望的信息

        :Example:
         - 例: message_box.check("提示信息")
        '''
        actual_message = self.get_message()
        try:
            Assertion(expected_message).check_string(actual_message)
            self.click_button(text=button_text)
        except:
            raise CsClientError(CustomError.Error_EQ03010001_Verify_Expect_Actual_Not_Equal,'界面取值为:{}，期望值为:{}，弹出的信息提示与期望信息不符！'.format(
                actual_message, expected_message))

    def check_match(self, expected_message, button_text="确定"):
        '''
        ucf wpf检查弹出消息框上的信息是否包含期望信息

        :Args:
         - expected_message：期望的信息

        :Example:
         - 例: message_box.check_match("提示信息")
        '''
        actual_message = self.get_message()
        try:
            Assertion(expected_message).match_string(actual_message)
            self.click_button(text=button_text)
        except:
            raise CsClientError(CustomError.Error_EQ03010001_Verify_Expect_Actual_Not_Equal,'界面取值为:{}，期望值为:{}，弹出的信息提示中不包含期望信息！'.format(
                actual_message, expected_message))

    def __get_button(self, text):
        '''
        ucf wpf获取弹出消息框上对应文本的按钮

        :Args:
         - text：按钮上的文本

        :Example:
         - 例: message_box.__get_button("确定")
        '''
        return Button({By.TEXT: text}, self.message_box, self.timeout)

    def click_button(self, text='确定'):
        '''
        ucf wpf 点击弹出消息框上对应文本的按钮

        :Args:
         - text：按钮上的文本

        :Example:
         - 例: message_box.click_button("确定")
        '''
        self.__get_button(text=text).click()
